package cn.texous.util.commons.util;

import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2019/6/18 11:09
 */
public class AuthorizationUtils {

    private static Map<String, String> AZ_MAP = new HashMap<>();

    /**
     * 获取access token
     *
     * @param url      请求路径
     * @param clientId client id
     * @param secret   secret
     * @return
     */
    public static AccessToken getAccessToken(String url, String clientId, String secret) {
        Map<String, String> header = new HashMap<>();
        header.put("Content-Type", "application/x-www-form-urlencoded");
        header.put("Accept", "application/json");
        header.put("Authorization", "Basic " + getAuthorizationStr(clientId, secret));
        //        url = url + "?grant_type=client_credentials";
        Map<String, String> outputStr = new HashMap<>();
        outputStr.put("grant_type", "password");
        MessageUtil.HttpsRequestParam param =
                MessageUtil.HttpsRequestParam.builder()
                        .header(header)
                        .requestMethod("POST")
                        .outputStr("grant_type=password")
                        .requestUrl(url)
                        .build();
        String result = MessageUtil.sendHttpsRequest(param);
        System.out.println(result);
        return new Gson().fromJson(result, AccessToken.class);
    }

    /**
     * getAuthorizationStr
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public static String getAuthorizationStr(String username, String password) {
        String authorization = AZ_MAP.get(username);
        if (authorization == null) {
            try {
                authorization = Base64.getEncoder().encodeToString(
                        String.format("%s:%s", username, password).getBytes("UTF-8"));
                AZ_MAP.put(username, authorization);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return authorization;
    }

    /**
     * 内部类存储 access token
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class AccessToken {
        private String scope;
        private String access_token;
        private String token_type;
        private String app_id;
        private String expires_in;
        private String nonce;
    }

    /**
     * 测试方法
     *
     * @param args 参数
     */
    public static void main(String[] args) {
        String url = "https://api.sandbox.paypal.com/v1/oauth2/token";
        String clientId = "";
        String secret = "";
        AccessToken accessToken = getAccessToken(url, clientId, secret);
        System.out.println(new Gson().toJson(accessToken));
    }


}
